<?php
/**
 * BHelper class.
 * BHelper is the helper static class for business portal.
 * 
 * It used for HTML printing functions.
 * 
 * LBB Solutions Oy 版权所有 @ 2013.
 */
class BHelper {
	/**
	 * 
	 */
	public static function HtmlHeaderMeta($controller) {
?>
	<title><?php echo CHtml::encode($controller->pageTitle); ?></title>
	<!-- Set the viewport "contet=width=device-width" for mobile -->
	<meta name="viewport" content="width=device-width, initial-scale=0.9" />
	<meta name="keywords" content="<?php echo CHtml::encode($controller->header['keywords']); ?>" />
	<meta name="description" content="<?php echo CHtml::encode($controller->header['description']); ?>" />
	<meta name="robots" content="noodp,noydir" />
	<link rel="canonical" href="<?php echo Yii::app()->params['siteRoot']; ?>" />
	<meta property="og:type" content="website" />
	<meta property="og:site_name" content="<?php echo Yii::app()->name; ?>" />
<meta property="og:title" content="<?php echo CHtml::encode($controller->header['title']); ?>" />
<meta property="og:description" content="<?php echo CHtml::encode($controller->header['description']); ?>" />
<meta property="og:url" content="<?php echo CHtml::encode($controller->header['url']); ?>" />
<meta property="og:image" content="<?php echo CHtml::encode($controller->header['image']); ?>" />
	<meta property="fb:app_id" content="<?php echo Yii::app()->params['facebook']['appId']; ?>" />
	<link href='http://fonts.googleapis.com/css?family=Open+Sans+Condensed:300,700|Open+Sans' rel='stylesheet' type='text/css' />
	<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/bootstrap.min.css?v=<?php echo Yii::app()->params['version']; ?>" />
	<style type="text/css">body {padding-top:100px; padding-bottom:40px;}</style>
	<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/bootstrap-responsive.min.css?v=<?php echo Yii::app()->params['version']; ?>" />
	<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/icons-style.css?v=<?php echo Yii::app()->params['version']; ?>" />
	<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/customized.css?v=<?php echo Yii::app()->params['version']; ?>" />
	<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
	<!-- Le fav and touch icons -->
    <link rel="shortcut icon" href="<?php echo Yii::app()->request->baseUrl; ?>/img/favicon.ico" />
    <link rel="apple-touch-icon-precomposed" sizes="144x144" href="<?php echo Yii::app()->request->baseUrl; ?>/img/logo.icon.144.png" />
    <link rel="apple-touch-icon-precomposed" sizes="114x114" href="<?php echo Yii::app()->request->baseUrl; ?>/img/logo.icon.114.png" />
    <link rel="apple-touch-icon-precomposed" sizes="72x72" href="<?php echo Yii::app()->request->baseUrl; ?>/img/logo.icon.72.png" />
    <link rel="apple-touch-icon-precomposed" href="<?php echo Yii::app()->request->baseUrl; ?>/img/logo.icon.57.png" />
	<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/js/jquery.min.js?v=<?php echo Yii::app()->params['version']; ?>"></script>
	<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/js/bootstrap.min.js?v=<?php echo Yii::app()->params['version']; ?>"></script>
<?php
$langcode = Yii::app()->language;
if ($langcode == 'zh_cn') $langcode = 'en';
?>
	<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&amp;libraries=places&amp;language=<?php echo $langcode; ?>"></script>
<?php if (!YII_DEBUG) { ?>
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-32570404-1']);
  _gaq.push(['_setDomainName', 'yamii.fi']);
  _gaq.push(['_trackPageview']);
  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
</script>
<?php } ?>
<?php
	}
	
	/**
	 * Mobile page header.
	 */
	public static function HtmlHeaderMobile($controller) {
?>
	<title><?php echo CHtml::encode($controller->pageTitle); ?></title>
	<!-- Set the viewport "contet=width=device-width" for mobile -->
	<meta name="viewport" content="width=device-width, initial-scale=0.9" />
	<link href='http://fonts.googleapis.com/css?family=Open+Sans+Condensed:300,700|Open+Sans' rel='stylesheet' type='text/css' />
	<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/bootstrap.min.css?v=<?php echo Yii::app()->params['version']; ?>" />
	<style type="text/css">body {padding-top:50px;}</style>
	<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/bootstrap-responsive.min.css?v=<?php echo Yii::app()->params['version']; ?>" />
	<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/icons-style.css?v=<?php echo Yii::app()->params['version']; ?>" />
	<!-- Google Fonts -->
	<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Ubuntu:300,400,400italic,700">
	<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans+Condensed:700">
	<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/customized.css?v=<?php echo Yii::app()->params['version']; ?>" />
	<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
	<!-- Le fav and touch icons -->
    <link rel="shortcut icon" href="<?php echo Yii::app()->request->baseUrl; ?>/img/favicon.ico" />
    <link rel="apple-touch-icon-precomposed" sizes="144x144" href="<?php echo Yii::app()->request->baseUrl; ?>/img/logo.icon.144.png" />
    <link rel="apple-touch-icon-precomposed" sizes="114x114" href="<?php echo Yii::app()->request->baseUrl; ?>/img/logo.icon.114.png" />
    <link rel="apple-touch-icon-precomposed" sizes="72x72" href="<?php echo Yii::app()->request->baseUrl; ?>/img/logo.icon.72.png" />
    <link rel="apple-touch-icon-precomposed" href="<?php echo Yii::app()->request->baseUrl; ?>/img/logo.icon.57.png" />
	<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/js/jquery.min.js?v=<?php echo Yii::app()->params['version']; ?>"></script>
	<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/js/bootstrap.min.js?v=<?php echo Yii::app()->params['version']; ?>"></script>
<?php
$langcode = Yii::app()->language;
if ($langcode == 'zh_cn') $langcode = 'en';
?>
	<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&amp;libraries=places&amp;language=<?php echo $langcode; ?>"></script>
<?php if (!YII_DEBUG) { ?>
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-32570404-1']);
  _gaq.push(['_setDomainName', 'yamii.fi']);
  _gaq.push(['_trackPageview']);
  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
</script>
<?php } ?>
<?php
	}
	
	/**
	 * print the ending scripts
	 */
	public static function HtmlScripts() {
		$fb_language_code = BUtils::GetFacebookLanguage(Yii::app()->language);
		$push_url = Yii::app()->createUrl('/accounts/push');
?>
<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/js/libs.min.js?v=<?php echo Yii::app()->params['version']; ?>"></script>
<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/js/yamii.js?v=<?php echo Yii::app()->params['version']; ?>"></script>
<?php if (YII_DEBUG) { ?>
<div style="position:fixed;bottom:20px;left:20px;">
	<a id="debugger" class="btn btn-danger" href="<?php echo Yii::app()->createUrl('/accounts/debug'); ?>">Debugger</a>
</div>
<script type="text/javascript">
$(function() {
	$('#debugger').colorbox();
});
</script>
<?php } ?>
<div id="fb-root"></div>
<script type="text/javascript">
function _push(d){$.post('<?php echo $push_url; ?>', d)}
$(function() {
	$('.btn-facebook, .facebook-login-action').click(function(){
		fbUI.login('<?php echo Yii::app()->createUrl('/accounts/facebook'); ?>');
	});
	$('.login-popup').colorbox({transition:'none'});
});
//Facebook
window.fbAsyncInit = function() {
	FB.init({appId:<?php echo Yii::app()->params['facebook']['appId']; ?>, status:true, cookie:true, xfbml:true});
	FB.Event.subscribe('edge.create', function(response) {
			_push({'key':'FB.Event.subscribe::edge.create', 'val':response});
		}
	);
};
(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) {return;}
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/<?php echo $fb_language_code; ?>/all.js";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<?php
	}
	
	
	/**
	 * Display a offer view
	 * @param array $r
	 */
	public static function HtmlOfferView($r) {
		$now_ts = time();
		
		//week string
		$week_array = BUtils::WeekDayKeys(7, true);

		//set offer picture
		$url_thumb_default = Yii::app()->baseUrl . '/img/default_special_thumb.png';
		$url_picture_default = Yii::app()->baseUrl . '/img/default_special_picture.png';
		$url_thumb = empty($r['url_picture']) ? $r['url_offer_img'] : $r['url_picture'];
		if (!empty($url_thumb)) {
			$url_thumb = BUtils::GetOfferPictureUrl($url_thumb);
			$url_picture = str_replace('thumb_', '', $url_thumb);
		} else {
			$url_thumb = $url_thumb_default;
			$url_picture = $url_picture_default;
		}
		
		//titles
		$valid_date_title = BUtils::T('special_valid_date_lbl');
		$offer_content_title = BUtils::T('special_content_lbl');
		$address_title = BUtils::T('address_lbl');

		//set price, discount or highlight
		$highlight = $del_price = '';
		if (!empty($r['price'])) {
			$highlight = MerchantObject::formatPrice($r['price']);
			if (!empty($r['value'])) {
				$del_price = '<span class="oldprice label">€'.$r['value'].'</span>';
			}
		} elseif (!empty($r['discount'])) {
			$highlight = MerchantObject::formatDiscount($r['discount']);
		}
		
		//define hours list of offer
		$a24Hours = BUtils::AllHour24();
		//set the offer_hours array
		$using_old_time = true;
		$offer_hours = array();
		for ($i=1; $i<8; $i++) {
			$field_start = "day_{$i}_start_hour";
			$field_end = "day_{$i}_end_hour";
			$offer_hours[$i] = array(
				'start_hour' => $r[$field_start],
				'end_hour'=> $r[$field_end],
			);
			if ($r[$field_start] !== null)  {
				$using_old_time = false;
			}
		}
		if($using_old_time) {
			//use the old way
			foreach (explode(',', $r['week_days']) as $weekNo) {
				$offer_hours[$weekNo] = array('start_hour'=>$r['start_hour'], 'end_hour'=>$r['end_hour']);
			}
		}
		$hours_list = MerchantObject::HtmlOfferHours($offer_hours, $week_array, $a24Hours);
		
		//set day string
		$week_string = BUtils::GetWeekdays($r['week_days'], $week_array);
		$range_date = BUtils::GetRecentDayName($r['week_days'], $week_string, false, $r['start_hour']);
		$period_string = BUtils::GetUntilDate($r['uts_end_date']);

		//set the valid_time
		$range_time = BUtils::GetHourRangeFormat($r['start_hour'], $r['end_hour'], $r['show_hours']);

		//business icon url
		$biz_icon = Yii::app()->baseUrl . '/img/icons/icon_others.png';
		if (!empty($r['category_key'])) {
			$biz_icon = Yii::app()->baseUrl . '/img/icons/icon_'.$r['category_key'].'.png';
		}

		//offer content
		$content = nl2br(strip_tags(trim($r['content'])));

		$max_number_msg = '';
		if ($r['max_number'] > 0) {
			$left_number = $r['max_number'] - $r['claimed_num'];
			$max_number_msg = '<div class="remaining">' . BUtils::T('offer_remaining_lbl', array('{number}' => $left_number)) . '</div>';
		}

		//address
		$biz_address = '';
		if (!empty($r['address'])) {
			$biz_address = $r['address'];
			if (!empty($r['biz_area'])) $biz_address .= ', ' . $r['biz_area'];
			if (!empty($r['postcode'])) $biz_address .= '<br />' . $r['postcode'] . ' ' . $r['city'];
		}

		//how to use tips: $r['redeem_type'] == 1 // in web version, onlt click to redeem
		$detect = new Mobile_Detect();
		if ($detect->isMobile() or YII_DEBUG) {
			$redeem_url = Yii::app()->createUrl('/special/redeem', array('id'=>$r['id']));
			$how_to_redeem_icon = '<img src="'.Yii::app()->baseUrl.'/img/icons/using_show.png" alt="" />';
			$how_to_redeem_btn = '<button class="btn btn-info" onclick="YM.redeem(\''.$redeem_url.'\', this)">'.BUtils::T('redeem_btn').'</button>';
			$how_to_redeem_msg = BUtils::T('special_using_click_lbl');
		} else {
			$how_to_redeem_icon = '<img src="'.Yii::app()->baseUrl.'/img/icons/using_show.png" alt="" />';
			$how_to_redeem_btn = '';
			$how_to_redeem_msg = BUtils::T('special_using_show_lbl');
		}
		$stamped_icon_url = Yii::app()->baseUrl . '/img/offer_unlocked.png';
		$stamped_icon_style = 'display:none;'; 
		if ($r['redeemed_status'] == 2) {
			$how_to_redeem_msg = BUtils::T('special_unlocked_eg', array('{time}' => date('H:i', $r['redeemed_time'])));
			$how_to_redeem_btn = '';
			$stamped_icon_style = 'display:block;';
		}
		
		echo <<<HTML
<div class="offer_v2" style="width:auto;">
	<div class="offer-header relative">
		<img src="{$url_picture}" alt="" onError="this.onerror=null;this.src='{$url_picture_default}';" />
		<div class="info">
			<div class="mb_price">
				<div class="highlight">{$highlight}</div>
				{$del_price}
			</div>
			<div class="mb_period">
				<div class="time">{$range_time}</div>
				<div class="date">{$range_date}</div>
			</div>
			<div class="clearfix"></div>
		</div>
	</div>
	<div class="action">
		<div class="margin">
			<div class="icon">{$how_to_redeem_icon}</div>
			<div class="redeem_button">{$how_to_redeem_btn}</div>
			<div class="tips">{$how_to_redeem_msg}</div>
			<div class="clearfix"></div>
		</div>
	</div>
	<ul class="offer-detail">
		<li class="field clearfix">
			<i class="sign icon-time icon-large"></i>
			<div class="desc">
				<span>{$hours_list}</span>
				<hr class="hr-2" />
				<span>{$period_string}<span>
			</div>
		</li>
		<li class="field clearfix">
			<div class="desc">{$content}</div>
		</li>
		<li class="field clearfix">
			<i class="sign icon-map-marker icon-large"></i>
			<div class="desc">{$biz_address}</div>
		</li>
		{$max_number_msg}
	</ul>
</div>
HTML;
	}
}